# 钩子框架 (类eBPF架构) Makefile

CC = gcc
CFLAGS = -Wall -Wextra -fPIC -g -O2 -D_GNU_SOURCE
LDFLAGS = -shared -ldl -pthread -lssl -lcrypto

# 输出目录
BIN_DIR = bin
LIB_DIR = lib
OBJ_DIR = obj

# 源文件目录
SRC_DIR = src
INCLUDE_DIR = include
PLUGINS_DIR = plugins

# 核心文件
CORE_SRC = $(SRC_DIR)/main.c \
           $(SRC_DIR)/hook_lib.c \
           $(SRC_DIR)/core/hook_core.c \
           $(SRC_DIR)/core/hook_interpose.c \
           $(SRC_DIR)/util/hook_helpers.c \
           $(SRC_DIR)/util/encrypt.c

# 对象文件
CORE_OBJ = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(CORE_SRC))

# 目标库
HOOK_LIB = $(LIB_DIR)/libhook.so

# 示例插件
SIMPLE_PLUGIN = $(PLUGINS_DIR)/simple_plugin/plugin.so
HELPER_PLUGIN = $(PLUGINS_DIR)/example_helper_plugin/plugin.so
EXAMPLE_PLUGIN = $(PLUGINS_DIR)/example_plugin/plugin.so

# 所有目标
all: dirs $(HOOK_LIB) plugins

# 创建必要的目录
dirs:
	@mkdir -p $(BIN_DIR) $(LIB_DIR) $(OBJ_DIR)
	@mkdir -p $(OBJ_DIR)/core $(OBJ_DIR)/util

# 编译钩子库
$(HOOK_LIB): $(CORE_OBJ)
	@mkdir -p $(LIB_DIR)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
	@echo "已构建钩子库: $@"

# 编译插件
plugins: $(SIMPLE_PLUGIN) $(HELPER_PLUGIN) $(EXAMPLE_PLUGIN)

# 通用对象文件编译规则
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -I$(INCLUDE_DIR) -c $< -o $@

# 插件编译规则
$(SIMPLE_PLUGIN):
	@echo "构建简单插件..."
	$(MAKE) -C $(PLUGINS_DIR)/simple_plugin

$(HELPER_PLUGIN):
	@echo "构建辅助工具插件..."
	$(MAKE) -C $(PLUGINS_DIR)/example_helper_plugin

$(EXAMPLE_PLUGIN):
	@echo "构建示例插件..."
	$(MAKE) -C $(PLUGINS_DIR)/example_plugin

# 清理编译产物
clean:
	rm -rf $(BIN_DIR) $(LIB_DIR) $(OBJ_DIR)
	$(MAKE) -C $(PLUGINS_DIR)/simple_plugin clean
	$(MAKE) -C $(PLUGINS_DIR)/example_helper_plugin clean
	$(MAKE) -C $(PLUGINS_DIR)/example_plugin clean
	@echo "已清理所有编译产物"

# 安装到系统
install: all
	@mkdir -p /usr/local/lib/hook
	@mkdir -p /usr/local/include/hook
	@cp $(HOOK_LIB) /usr/local/lib/
	@cp $(INCLUDE_DIR)/*.h /usr/local/include/hook/
	@ldconfig
	@echo "钩子框架已安装到系统"

# 卸载
uninstall:
	@rm -f /usr/local/lib/libhook.so
	@rm -rf /usr/local/include/hook
	@ldconfig
	@echo "钩子框架已从系统卸载"

# 构建测试程序
test: all
	$(CC) $(CFLAGS) -o $(BIN_DIR)/test_app examples/test_app.c -I$(INCLUDE_DIR) -lssl -lcrypto
	$(CC) $(CFLAGS) -o $(BIN_DIR)/advanced_test examples/advanced_test.c -I$(INCLUDE_DIR)
	$(CC) $(CFLAGS) -o $(BIN_DIR)/helper_test examples/helper_test.c -I$(INCLUDE_DIR)
	@echo "测试程序已构建完成"

# 运行测试
run-test: test
	@echo "运行测试程序..."
	LD_PRELOAD=$(HOOK_LIB) $(BIN_DIR)/test_app

run-advanced: test
	@echo "运行高级测试程序..."
	LD_PRELOAD=$(HOOK_LIB) $(BIN_DIR)/advanced_test

run-helper: test
	@echo "运行辅助测试程序..."
	LD_PRELOAD=$(HOOK_LIB) HOOK_PLUGIN_DIR=$(PLUGINS_DIR)/example_helper_plugin $(BIN_DIR)/helper_test

# 调试帮助
help:
	@echo "可用目标:"
	@echo "  all          - 构建钩子库和所有插件"
	@echo "  plugins      - 仅构建插件"
	@echo "  clean        - 清理所有编译产物"
	@echo "  install      - 安装钩子框架到系统"
	@echo "  uninstall    - 从系统卸载钩子框架"
	@echo "  test         - 构建测试程序"
	@echo "  run-test     - 运行基础测试程序"
	@echo "  run-advanced - 运行高级测试程序"
	@echo "  run-helper   - 运行辅助测试程序"
	@echo ""
	@echo "环境变量:"
	@echo "  HOOK_LOG_LEVEL    - 设置日志级别 (0=ERROR, 1=WARN, 2=INFO, 3=DEBUG)"
	@echo "  HOOK_PLUGIN_DIR   - 设置插件目录"
	@echo "  HOOK_ENABLED_GROUPS - 设置启用的钩子组 (逗号分隔)"

.PHONY: all dirs clean install uninstall test run-test run-advanced run-helper plugins help
